<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '角色添加' : '角色修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      user:{{ user }}
      <el-form :model="user">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="user.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" label-width="120px">
          <!-- 
      data：展示的数据
      :props="{ children: '判断是否有子节点的字段', label: '页面展示的字段' }"
      @node-click 点了节点，可以获取到点击的节点的数据
     -->
          <el-tree
            :data="treeData"
            :props="{ children: 'children', label: 'title' }"
            show-checkbox
            node-key="id"
            ref="tree"
          ></el-tree>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  reqmenulist,
  reqroleadd,
  reqroleedit,
  reqroleinfo,
} from "../../../http/api";
import { formClose } from "../../../mixins";
import { successAlert } from "../../../utils/alert";
export default {
  // 4.接收info
  props: ["info", "arr"],
  mixins: [formClose],
  data() {
    return {
      treeData: [],

      // 5.1 初始化表单数据
      user: {
        rolename: "",
        menus: "",
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      userInfo:"userInfo"
    }),
  },
  mounted() {
    // 请求树的数据
    reqmenulist({ istree: true }).then((res) => {
      if (res.data.code == 200) {
        this.treeData = res.data.list ? res.data.list : [];
      }
    });
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"
    }),
    
    // 清空user
    empty() {
      this.user = {
        rolename: "",
        menus: "",
        status: 1,
      };
      //清除树的数据
       this.$refs.tree.setCheckedKeys([])
    },
    // 5.4 点了添加
    add() {
      // 先获取树的数据，并赋值给user.menus
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());

      reqroleadd(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.$emit("init");
        }
      });
    },
    
    // 8. 获取一条数据
    getOne(id) {
      reqroleinfo({ id: id }).then((res) => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          // 补id
          this.user.id = id;
          // 给树赋值
          this.$refs.tree.setCheckedKeys(JSON.parse(this.user.menus))
        }
      });
    },
    // 9.修改
    update() {
       // 先获取树的数据，并赋值给user.menus
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // 如果当前修改的角色的id （user.id） 和当前用户所属的角色的id（userINFo.roleid）一致,修改成功，要清除数据并退出
          if(this.user.id==this.userInfo.roleid){
            this.changeUserInfo({})
            this.$router.push("/login")
            return;
          }

          // ②弹框内容清空
          this.empty();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          this.$emit("init");
        }
      });
    },
   
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>